package cn.topcodes.tcsf.app.service.impl;

import cn.topcode.unicorn.utils.StringUtil;
import cn.topcodes.tcsf.app.domain.api.PushForceLogout;
import cn.topcodes.tcsf.app.domain.entity.AppClient;
import cn.topcodes.tcsf.app.event.AppUserLoginEvent;
import cn.topcodes.tcsf.app.event.ForceLogoutEvent;
import cn.topcodes.tcsf.app.service.PushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import static cn.topcodes.tcsf.app.service.impl.AppClientServiceImpl.PUSH_TYPE_FORCED_LOGOUT;

/**
 * Created by Unicorn on 2018/6/22.
 */
@Component
public class ForceLogoutListener implements ApplicationListener<ForceLogoutEvent> {

    @Autowired
    private PushService pushService;

    @Override
    public void onApplicationEvent(ForceLogoutEvent event) {
        String originToken = event.getOriginToken();
        AppClient appClient = event.getAppClient();

        //  通知其他设备账号在新设备登录
        if(StringUtil.isNotBlank(originToken)) {
            PushForceLogout obj = new PushForceLogout();
            obj.setApp(appClient.getApp());
            obj.setAppUserId(appClient.getAppUserId());
            obj.setLoginTime(appClient.getLastActiveTime());
            obj.setPlatform(appClient.getPlatform());
            obj.setTerminal(appClient.getTerminal());
            this.pushService.pushByToken(originToken, PUSH_TYPE_FORCED_LOGOUT, obj);
        }
    }
}
